<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>
      <a-form-item
        label="仓库"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-tree-select
          style="width: 100%"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="storeTreeDate"
          :treeNodeFilterProp="'title'"
          :showSearch="true"
          v-decorator="['storeId', {rules: [{required: false, message: '仓库库位不能为空'}]}]"
          placeholder="请选择"
        >
        </a-tree-select>
      </a-form-item>
      <a-form-item
        label="备件id"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
        v-show="false"
      >
        <a-input
          v-decorator="['spareId', {rules: [{required: true, message: '备件id不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="备件名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          disabled
          style="width: 70%"
          v-decorator="['spareName', {rules: [{required: true, message: '备件名称不能为空'}]}]" />
        <a-button type="primary" @click="handleSpareSelect">选择备件</a-button>

      </a-form-item>
      <a-form-item
        label="数量"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          style="width: 100%"
          :min="0"
          v-decorator="['num', {rules: [{required: true, message: '数量不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="价格"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          style="width: 100%"
          :min="0"
          v-decorator="['price', {rules: [{required: true, message: '数量不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="初始价"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          style="width: 100%"
          :min="0"
          v-decorator="['initPrice', {rules: [{required: true, message: '初始价不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="采购价"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          style="width: 100%"
          :min="0"
          v-decorator="['initPurchasePrice', {rules: [{required: true, message: '采购价不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="排序"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          style="width: 100%"
          :min="1"
          v-decorator="['sort', {initialValue:1,rules: [{required: true, message: '排序不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="是否删除"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select v-decorator="['delFlag', {initialValue:DictCache.VALUE.DELFLAG.NORMAL,rules: [{required: true, message: '是否删除不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in delFlagMap"
            :key="value"
            :label="label"
            :value="parseInt(value)">{{ label }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="备注"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-textarea
          :rows="4"
          v-decorator="['remark', {rules: [{required: false, message: '备注不能为空'}]}]"/>
      </a-form-item>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
    <store-select-modal ref="storeSelectModal" @selected="handleStoreSelected"/>
    <spare-part-info-select-modal ref="spareSelectModal" @selected="handleSpareSelected"/>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addSpareStore, updateSpareStore } from '@/api/store/sparestore'
import StoreSelectModal from '@/views/store/store/modules/StoreSelectModal'
import SparePartInfoSelectModal from '@/views/sqarepartmanage/sparepartinfo/modules/SparePartInfoSelectModal'
import { fetchStoreTree } from '@/api/store/store'

export default {
  name: 'BaseSpareStore',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      storeTreeDate: [],
      // 下拉框map
      delFlagMap: {}
    }
  },
  components: {
    StoreSelectModal,
    SparePartInfoSelectModal
  },
  props: {
  },
  created () {
    this.setTree()
    // 下拉框map
    this.delFlagMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.DELFLAG)
  },
  methods: {
    /**
     * 设置仓库、备件类别树
     */
    setTree () {
      fetchStoreTree().then(res => {
        this.storeTreeDate = this.BaseTool.TREE.treeFilter(res.data, (item) => {
          console.log(item.title + ',,' + item.level)
          if (item.level === this.DictCache.VALUE.STORE_LEVEL.ZONGCANG) {
            console.log(false)
            item.selectable = false
            item.disabled = true
          }
        })
      })
    },
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'spareId',
          'spareName',
          'storeId',
          'storeName',
          'batchId',
          'batchUserId',
          'num',
          'price',
          'initPrice',
          'initPurchasePrice',
          'sort',
          'delFlag',
          'remark'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addSpareStore(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateSpareStore(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    },
    handleStoreSelect () {
      this.$refs.storeSelectModal.base({}, {})
    },
    handleStoreSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'storeId': key,
          'storeName': row.name
        }))
      })
    },
    handleSpareSelect () {
      this.$refs.spareSelectModal.base()
    },
    handleSpareSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'spareId': key,
          'spareName': row.name,
          'price': row.initialValue,
          'initPrice': row.initialValue,
          'initPurchasePrice': row.initialValue
        }))
      })
    }

  }
}
</script>
